<template>
	<view class="pageview">
		<cl-navbar :title="$t('my_file.collect.index.title')" leftIconSize="40rpx" placeholder autoBack>
			<template slot="right">
				<text style="font-size: 28rpx;" @click="manageClick" v-if="type != 'chat'">{{isManage?$t("common.cancelText"):$t("addressbook.friend_group.manage")}}</text>
			</template>
		</cl-navbar>
		<view class="tabsview">
			<u-tabs :list="tabs" @click="tabClick" :activeStyle="{ background: 'linear-gradient(180deg, rgba(255, 255, 255, 1) 0, rgba(182, 139, 77, 1) 100%)','-webkit-background-clip': 'text',
					'-webkit-text-fill-color': 'transparent'}" :inactiveStyle="{ color: '#61626e' }" lineHeight="10rpx"
				lineWidth="10rpx"
				lineColor="linear-gradient(180deg, rgba(255, 255, 255, 1) 0, rgba(182, 139, 77, 1) 100%)"></u-tabs>
		</view>
		
		<view class="pageContent">
			<z-paging ref="paging" v-model="indexList" width="690rpx" height="100%" :fixed="false" @query="queryList" :show-loading-more-no-more-view="false" 
				use-safe-area-placeholder inside-more>
				<view class="m-srcollview">
					<view v-for="(item,index) in indexList" :key="index" class="contentView">
						<view style="display: flex;align-items: flex-end;">
							<view style="font-size: 32rpx;color: #FFFFFF;">{{item.create_timeJson.day}}</view>
							<view style="font-size: 24rpx;color: #595A66;">/{{item.create_timeJson.month}}</view>
							<view style="font-size: 24rpx;color: #595A66;" v-if="item.create_timeJson.year">/{{item.create_timeJson.year}}</view>
						</view>
						<view class="resourceView">
							<view class="imageView" style="position: relative;" @click="imageClick(item)">
								<!-- <view style="position: absolute;left: 0;right: 0;top: 0;bottom: 0;z-index: 2;" v-if="type == 'chat'"  @click="imageClick(item)"></view> -->
								<cl-chat-img-video :imgList="mediaAttachments(item.content)" :maxWidth="400" :max-height="400" :isPreview="false" :noclickVideo="true" :noclick="true" v-if="type == 'chat'"></cl-chat-img-video>
								<cl-chat-img-video :imgList="mediaAttachments(item.content)" :maxWidth="400" :max-height="400" v-else></cl-chat-img-video>
							</view>
							<view class="cellview__right" @click.stop="delGroup(item,index)" v-if="isManage">
								<u-icon name="minus-circle-fill" color="#ff4242" size="20"></u-icon>
							</view>
						</view>
					</view>
				</view>
			</z-paging>
		</view>
		<view>
			<cl-model ref="clShare">
				<view class="shareView" v-if="chatInfo && chatdata">
					<text class="shareView__title">{{$t("chat.business_share.Sendto")}}</text>
					<view class="shareView__top">
						<cl-avatar
							:src="chatInfo.avatar_url"
							size="60rpx"></cl-avatar>
						<text class="shareView__top__name">{{chatInfo.nickname}}</text>
					</view>
					<view class="shareView__content">
						<view class="content" v-if="chatdata.msg_type == 'image'">
							<cl-chat-img-video :imgList="chatdata.content_text" width="400"></cl-chat-img-video>
						</view>
						<view class="content" v-else-if="chatdata.msg_type == 'video'">
							<cl-chat-img-video :imgList="chatdata.content_text" width="400"></cl-chat-img-video>
						</view>
					</view>
				</view>
			</cl-model>
		</view>
	</view>
</template>

<script>
	import { getDateParts } from '@/common/chatMsgTime.js'
	import {
		v4 as uuidv4
	} from 'uuid';
	import {
		mapState
	} from 'vuex'
	import favoritesApi from '@/services/api/favorites.js'
	import messageApi from '@/services/api/msg.js'
	export default {
		name: 'collect',
		data() {
			return {
				type:'',//页面作用
				
				tabs: [{
					name: this.$t("chat.msg.type.image"),
					type:'image',
				}, {
					name: this.$t("chat.msg.type.video"),
					type:'video',
				}],
				indexList:[],
				favorite_type:'image',
				isManage:false,
				chatdata:null
			}
		},
		onLoad() {
			let query = this.$Route.query
			if(query.type){
				this.type = query.type
			}
		},
		computed: {
			...mapState({
				chatInfo:state => state.chat.chatInfo
			}),
			mediaAttachments(){
				return (item)=>{
					if(item.msg_type == 'image'){
						return [{
						    image: {
						        url: item.url,
						        width: item.width,
						        height: item.height,
								thumb_url:item.thumb_url,
						        msg_order: 0
						    }
						}]
					}else{
						return [{
						    "video": {
						        url: item.url,
						        width: item.width,
						        height: item.height,
								duration:item.duration,
						        thumb_url:item.thumb_url,
						        msg_order: 0
						    }
						}]
					}
				}
			}
		},
		onUnload(){
			//清空一下放大图片的存储
			this.$store.commit('setImages',[])
		},
		methods: {
			async imageClick(item){
				let msgdata = [];
				if(item.content.msg_type == 'image'){
					msgdata.push({
						image: {
							url: item.content.url,
							thumb_url: item.content.thumb_url,
							width: item.content.width,
							height: item.content.height,
							msg_order: msgdata.length
						}
					})
				}else if(item.content.msg_type == 'video'){
					msgdata.push({
						video: {
							url: item.content.url,
							thumb_url: item.content.thumb_url,
							width: item.content.width,
							height: item.content.height,
							msg_order: msgdata.length,
							duration: item.content.duration, // 假设视频时长为20秒
						}
					})
				}
				let wy_id = uuidv4();
				this.chatdata = {
					  target_id: this.chatInfo.uuid,
					  local_id: wy_id,
					  msg_type: item.content.msg_type,
					  content_text: msgdata
				}
				this.$refs.clShare.show({
					confirmText:this.$t("common.sendConfirm"),
					cancelText:this.$t("common.cancelText"),
					confirm:async (e)=> {
						try {
							await messageApi.directsend(this.chatdata, this.chatInfo.chat_type);
							uni.showToast({
								title:this.$t("tost.title.sendOk"),
								icon:'none'
							})
							e.showModel = false
							setTimeout(()=>{
								uni.navigateBack({
								  delta: 1,  // 返回的页面数，1 表示返回到上一个页面
								});
							},1000)
						} catch (err) {}
					},
					close:()=> {
						//清空一下放大图片的存储
						this.$store.commit('setImages',[])
					}
				})
			},
			manageClick(){
				this.isManage = !this.isManage
			},
			async delGroup(item,index){
				try{
					let res = await favoritesApi.del({
						favorite_ids:[item.id]
					})
					if(res){
						this.indexList.splice(index,1)
					}
				}catch(e){
					
				}
			},
			async queryList(pageNo, pageSize) {
				try {
					let res = await favoritesApi.list({
						page: pageNo,
						limit: pageSize,
						favorite_type:this.favorite_type
					})
					if (res) {
						res.list.map(item=>{
							item.create_timeJson = {}
							item.create_timeJson = getDateParts(item.create_time)
						})
						this.$refs.paging.complete(res.list)
					} else {
						this.$refs.paging.complete(false)
					}
				} catch (e) {
					this.$refs.paging.complete(false)
				}
			},
			tabClick(e){
				this.favorite_type = e.type
				this.$refs.paging.reload()
			}
		},
	};
</script>

<style scoped lang="scss">
	.pageview{
		display: flex;
		flex-direction: column;
	}
	.searchview {
		margin: 20rpx 30rpx;
		background-color: #ffffff;
		border-radius: 60rpx;
		padding-right: 20rpx;
	}
	.tabsview{
		margin-left: 50rpx;
	}
	.pageContent {
		margin: 20rpx 30rpx;
		box-sizing: border-box;
		border-radius: 20rpx;
		flex: 1;
		background: #ffffff;
		.m-srcollview{
			padding: 0 30rpx 30rpx;
			border-radius: 20rpx;
			overflow: hidden;
			.contentView{
				margin-top: 30rpx;
				.resourceView{
					margin-top: 20rpx;
					margin-left: 70rpx;
					display: flex;
					justify-content: space-between;
					align-items: center;
				}
				
			}
		}
	}
	.shareView{
		width: 100%;
		text-align: left;
		&__title{
			font-size: 28rpx;
			font-weight: bold;
		}
		&__top{
			display: flex;
			align-items: center;
			margin-top: 20rpx;
			&__name{
				margin-left: 10rpx;
				font-size: 26rpx;
			}
		}
		&__content{
			margin-top: 30rpx;
			padding-left: 15rpx;
			&__text{
				font-size: 26rpx;
			}
		}
	}
</style>